package com.sly.demo.mapstruct.model.converter;

import com.sly.demo.mapstruct.model.dto.UserDto;
import com.sly.demo.mapstruct.model.po.User;
import com.sly.demo.mapstruct.model.vo.UserVo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

/**
 * 用户转换器
 * @author SLY
 * @date 2022/8/7
 */
@Mapper(unmappedSourcePolicy = ReportingPolicy.WARN, unmappedTargetPolicy = ReportingPolicy.WARN)
public interface UserConverter {

    UserConverter INSTANCE = Mappers.getMapper(UserConverter.class);

    /**
     * 用户实体转用户DTO
     * @param user 用户实体
     * @return {@link UserDto}
     */
    @Mappings({
            @Mapping(target = "orderList", ignore = true),
    })
    UserDto userToUserDto(User user);

    /**
     * 用户Dto转用户Vo
     * @param userDto 用户Dto
     * @return {@link UserVo}
     */
    @Mappings({
            @Mapping(expression ="java(CommonExpression.parseStatusToStatusStr(userDto.getStatus()))", target = "status"),
            @Mapping(expression = "java(OrderConverter.INSTANCE.orderDtoToOrderVoList(userDto.getOrderList()))", target = "orderList"),
    })
    UserVo userDtoToUserVo(UserDto userDto);
}
